<?php
/**
 * Methods for displaying presentation data using PHPTAL (http://phptal.motion-twin.com/)
 *
 *
 * PHP 5 ONLY !!!!
 *
 * Copyright (c) 2007, Daniel Kvasnicka
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c) 2007, Daniel Kvasnicka
 * @link			
 * @package		cake
 * @subpackage	cake.app.views
 * @version		0.1
 * @lastmodified	$Date: 2006-12-03 23:29:12 +0100 $
 * @license		http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Included libraries.
 */
App::import('Vendor', 'PHPTAL', array('file' => 'PHPTAL-1.2.1'.DS.'PHPTAL.php'));
/**
 * PHPTALView, the V in the MVC triad, made the PHPTAL way.
 *
 * Class holding methods for displaying presentation data.
 *
 * @package			cake
 * @subpackage		cake.app.views
 */
class PhptalView extends View {

	/**
	 * PHPTALView constructor
	 *
	 * @param  $controller instance of calling controller
	 */
	function __construct(&$controller) {
		parent::__construct($controller);
		
		$this->ext = ".zpt";
		$this->template = new PHPTAL();
	}

/**
 * Renders and returns PHPTAL template for given view with its array of data.
 *
 * @param string $___viewFn Filename of the view
 * @param array $___dataForView Data to include in rendered view
 * @return string Rendered output
 * @access protected
 */
	function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
		if ($this->helpers != false) {
			$loadedHelpers = array();
			$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);

			foreach(array_keys($loadedHelpers) as $helper) {
				$replace = strtolower(substr($helper, 0, 1));
				$camelBackedHelper = preg_replace('/\\w/', $replace, $helper, 1);

				${$camelBackedHelper} =& $loadedHelpers[$helper];

				if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) {
					foreach(${$camelBackedHelper}->helpers as $subHelper) {
						${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
					}
				}
				$this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
			}
		}
		
		$this->template->setTemplate( $___viewFn );
		
		foreach($___dataForView as $data => $value)
		{
			$this->template->set($data, $value);
		}		
		
		foreach($loadedHelpers as $data => $value)
		{
            $data = strtolower($data);
            if (!array_key_exists($data, $___dataForView)) {
    			$this->template->set($data, $value);
            }
		}		

        $this->template->set('this', $this);
		try {
		    return $this->template->execute();
		} catch (Exception $e){
		    return "<pre>".$e->__toString()."</pre>";
		}
	}
}
?>

